<template>
  <div class="login">
    <MyHead title="注册" :show="true"></MyHead>
    <div class="kong"></div>
    <van-form @submit="onSubmit"  ref="registerfrom">
      <van-field
        v-model="register.user"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写正确的用户名' }]"
      />
      <van-field
        v-model.trim="register.phone"
        name="phone"
        label="手机号码"
        placeholder="手机号码"
        :rules="[
          { required: true, message: '请填写手机号码' },
          { pattern: phonePattern, message: '请输入正确的手机号 ' },
        ]"
      />
      <van-field
        v-model="register.password"
        :type="type"
        name="password"
        label="密码"
        :right-icon="type=='password'?'closed-eye':'eye-o'"
        @click-right-icon="changeType"
        placeholder="必须带有字母和数字的6-18位"
        :rules="[ 
          { required: true, message: '请填写密码' },
          {
            pattern: $reg.pwd,
            message: '密码必须带有数字和英文且必须是6-18位',
          },
        ]"
        @blur="checkDbpass"
      />
      <van-field
        v-model="register.dbpass"
           :type="type1"
        name="dbpass"
        label="确认密码"
        placeholder="确认密码"
         :right-icon="type1=='password'?'closed-eye':'eye-o'"
        @click-right-icon="changeType1"
        :rules="[{ required: true, message: '请填写确认密码' },
        {
            validator:checkPassEqual,message:'两次密码输入不一致'  
        }
        ]"
      />
      <van-field name="agree" :rules="[{ required: true, message: '请同意' }]">
        <template #input>
          <van-checkbox v-model="register.agree"
            >请同意我们的霸王条款</van-checkbox
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      checked: true,
      register: {},
      phonePattern: this.$reg.phone,
      type:"password",
      type1:"password",
      
    };
  },
  methods: {
    onSubmit(val) {
        console.log(val);
        this.$ajax.register(val)
        .then(res=>{
          if(res.code == 200){
            this.$router.push({name:'login',query:{phone:val.phone}})
            
          }
        })
        .catch(err=>{
          
        })
    },
    checkPassEqual(val){
        return this.register.password ==  val
    },
    changeType(){
        this.type = this.type == 'password' ? 'type' :'password'
    },
     changeType1(){
            this.type1 = this.type1 =='password' ?'type':'password'
        },
    checkDbpass(){
        if(this.register.dbpass){
            this.$refs.registerfrom.validate("dbpass")
        }
    }

  },
};
</script>

<style  lang="scss" scoped>
.login {
  background: url("../wr.jpg") no-repeat 50% 50%;
  background-size: cover;
   width: 100%;
        height: 100%;
  ::v-deep .van-cell {
    // background: rgba(255, 255, 255,0.5);

    background: rgba(255, 255, 255, 0.8);
 
  
  }

  ::v-deep .van-tab--active {
    .van-tab__text {
      color: #000;
    }
  }
 ::v-deep .van-tabs__content{
    margin-top: 200px;
  }
}
.kong{
  margin-top: 200px;
}
</style>